package com.macro.mall.portal.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.UmsMember;
import com.macro.mall.model.UmsMemberReceiveAddress;
import com.macro.mall.portal.dto.ums.InsertMemberAddressReqDto;
import com.macro.mall.portal.dto.ums.UpdateMemberAddressReqDto;
import com.macro.mall.portal.service.UmsMemberReceiveAddressService;
import com.macro.mall.ums.entity.MemberAddressEntity;
import com.macro.mall.ums.service.address.MemberAddressService;
import com.macro.mall.ums.service.address.bo.InsertMemberAddressInput;
import com.macro.mall.ums.service.address.bo.UpdateMemberAddressInput;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员收货地址管理Controller
 * Created by macro on 2018/8/28.
 */
@Controller
@Api(tags = "UmsMemberReceiveAddressController", description = "会员收货地址管理")
@RequestMapping("/member/address")
public class UmsMemberReceiveAddressController extends BaseController {
    @Autowired
    private UmsMemberReceiveAddressService memberReceiveAddressService;

    @Autowired
    private MemberAddressService memberAddressService;

    @ApiOperation("添加收货地址")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult add(@RequestBody InsertMemberAddressReqDto reqDto) {
        UmsMember currentUser = getCurrentUser();
        InsertMemberAddressInput insertMemberAddressInput = new InsertMemberAddressInput();
        insertMemberAddressInput
                .setMemberId(currentUser.getId())
                .setName(reqDto.getName())
                .setPhoneNumber(reqDto.getPhoneNumber())
                .setDefaultStatus(reqDto.getDefaultStatus())
                .setPostCode(reqDto.getPostCode())
                .setProvince(reqDto.getProvince())
                .setCity(reqDto.getCity())
                .setRegion(reqDto.getRegion())
                .setDetailAddress(reqDto.getDetailAddress())
                .setAreaCode(reqDto.getAreaCode());
        long id = memberAddressService.insert(insertMemberAddressInput);

        return CommonResult.success(memberAddressService.getById(id));
    }

    @ApiOperation("删除收货地址")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        int count = memberReceiveAddressService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改收货地址")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestBody UpdateMemberAddressReqDto reqDto) {
        UpdateMemberAddressInput updateMemberAddressInput = new UpdateMemberAddressInput();
        updateMemberAddressInput
                .setId(id)
                .setName(reqDto.getName())
                .setPhoneNumber(reqDto.getPhoneNumber())
                .setDefaultStatus(reqDto.getDefaultStatus())
                .setPostCode(reqDto.getPostCode())
                .setProvince(reqDto.getProvince())
                .setCity(reqDto.getCity())
                .setRegion(reqDto.getRegion())
                .setDetailAddress(reqDto.getDetailAddress())
                .setAreaCode(reqDto.getAreaCode());

        memberAddressService.update(updateMemberAddressInput);

        return CommonResult.success(memberAddressService.getById(reqDto.getId()));
    }

    @ApiOperation("显示所有收货地址")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<MemberAddressEntity>> list() {
        UmsMember currentUser = getCurrentUser();

        List<MemberAddressEntity> addressList = memberAddressService.listByMemberId(currentUser.getId());
        return CommonResult.success(addressList);
    }

    @ApiOperation("显示所有收货地址")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<MemberAddressEntity> getItem(@PathVariable Long id) {
        MemberAddressEntity address = memberAddressService.getById(id);
        return CommonResult.success(address);
    }
}
